var fs = require('fs')
var rp = require('request-promise')
var cheerio = require('cheerio')
var tough = require('tough-cookie');

var config = require('./config.js')
var util = require('./util/_default.js')
var xlsx = require('node-xlsx')

var proxyUrl = '', trycount = 10
var page, maxpage, ip

var iplist = ['49.87.75.141:37420', '115.215.51.246:44451', '114.239.250.246:27848']

var helper = util.helper

var dt = [['序号', '流水号', '类别', '目录名称', '产品注册名称', '规格',
    '型号', '单位', '企业注册号', '投标企业', '生产企业', '操作']]

var dtdetail = [['商品流水号', '企业注册号', '分类名称', '目录名称', '二级分类名称', '产品注册信息', '规格', '型号', '描述', '包装规格', '产品材质', '包装单位', '品牌', '投标企业名称', '生产企业名称', '产品适用范围', '产品性能结构及组成', '其他说明', '方法学', '是否专机专用', '专机名称及型号', '计价单位', '测试人份', '是否有GMP', '是否提供校准品', '是否提供质控品','北京市中标价或挂网限价','重庆市中标价或挂网限价','天津市中标价或挂网限价','上海市中标价或挂网限价','西藏自治区中标价或挂网限价','新疆维吾尔自治区中标价或挂网限价','内蒙古自治区中标价或挂网限价','宁夏回族自治区中标价或挂网限价','广西壮族自治区中标价或挂网限价','河北省中标价或挂网限价','山西省中标价或挂网限价','辽宁省中标价或挂网限价','吉林省中标价或挂网限价','黑龙江省中标价或挂网限价','江苏省中标价或挂网限价','浙江省中标价或挂网限价','安徽省中标价或挂网限价','福建省中标价或挂网限价','江西省中标价或挂网限价','山东省中标价或挂网限价','河南省中标价或挂网限价','湖北省中标价或挂网限价','湖南省中标价或挂网限价','广东省中标价或挂网限价','海南省中标价或挂网限价','四川省中标价或挂网限价','贵州省中标价或挂网限价','云南省中标价或挂网限价','陕西省中标价或挂网限价','甘肃省中标价或挂网限价','青海省中标价或挂网限价','卫生部成交候选品种价']]

var lsh = []

var href = 'http://192.168.200.19:8086/ShenHeMessage/GongshiGoods.aspx'



function main() {

    var opts = helper.deepCopy(config.reqOpts)
    opts.url = href
    opts.method = 'post'
    opts.headers.Referer = 'http://192.168.200.19:8086/Controls/left.aspx'
    let cookie = new tough.Cookie({
        key: "ASP.NET_SessionId",
        value: "x3ltnieppqu0xf55oxy01r55",
        domain: '192.168.200.19',
        httpOnly: true,
        maxAge: 31536000
    },{
        key: "CheckCode",
        value: "3441",
        domain: '192.168.200.19',
        httpOnly: true,
        maxAge: 31536000
    });
    
    var cookiejar = rp.jar();
    cookiejar.setCookie(cookie, 'http://192.168.200.19');
    opts.jar = cookiejar

    rp(opts).then(function (body) {
        var $ = cheerio.load(body)
        var data = helper.getFormData($('form#form1').serializeArray())
        page = 1
        GetDetail(data);
    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            main()
        }, Math.random() * 10 * 1000)
    })
}



function GetDetail(data, isnext) {
    console.log('正在抓取第' + page + '页,当前总数：' + dt.length)
    var opts = helper.deepCopy(config.reqOpts)
    opts.url = href
    opts.method = 'post'
    opts.headers.Referer = href
    opts.jar = cookiejar
    //console.log(data)
    data['__EVENTARGUMENT'] = page
    //data['__VIEWSTATEGENERATOR'] = 'FEC5FE02'
    opts.formData = data

    opts.proxy = 'http://' + ip

    rp(opts).then(function (body) {
        var $ = cheerio.load(body)
        var tr = $('tr', 'table.mainlist')
        if (tr.length > 0) {
            tr.slice(1).each(function () {
                var row = []
                row.push(
                    iGetInnerText($("td", this).eq(0).text()),
                    iGetInnerText($("td", this).eq(1).text()),
                    iGetInnerText($("td", this).eq(2).text()),
                    iGetInnerText($("td", this).eq(3).text()),
                    iGetInnerText($("td", this).eq(4).text()),
                    iGetInnerText($("td", this).eq(5).text()),
                    iGetInnerText($("td", this).eq(6).text()),
                    iGetInnerText($("td", this).eq(7).text()),
                    iGetInnerText($("td", this).eq(8).text()),
                    iGetInnerText($("td", this).eq(9).text()),
                    iGetInnerText($("td", this).eq(10).text()),
                    iGetInnerText($("td", this).eq(11).text())
                )
                lsh.push(iGetInnerText($("td", this).eq(1).text()));
                dt.push(row)
            });
            if ((dt.length - 1) % 300 == 0) {
                saveExcel(dt, page)
            }
            console.log(dt)
            console.log(lsh)
            //syncdetail()
            /* var next = $('#lbtnnextpage').attr('disabled')
            if (typeof (next) == "undefined") {
                page = page + 1
                GetDetail(data, cookiejar);
            }
            else {
                saveExcel(dt, page)
            } */
        }
    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            main()
        }, Math.random() * 10 * 1000)
    })

}

function syncdetail() {
    if (lsh.length > 0) {
        GetDetail(lsh.shift())
    } else {
        page = page + 1
        main(page)
    }
}


function GetDetail(id) {
    console.log('正在抓取流水号' + id + '，当前总数：' + dtdetail.length)
    var opts = helper.deepCopy(config.reqOpts)
    opts.url = 'http://192.168.200.19:8086/ShenHeMessage/GongshiGoodsInfoSJ.aspx?id=' + id
    opts.method = 'Get'
    opts.headers.Referer = 'http://192.168.200.19:8086/ShenHeMessage/GongshiGoods.aspx'
    opts.jar = cookiejar

    rp(opts).then(function (body) {
        var $ = cheerio.load(body)
        //console.log(body)
        var td = $('span')
        if (td.length > 0) {
            var row = []
            td.each(function () {
                row.push(
                    iGetInnerText($(this).text())
                )
            });
            dtdetail.push(row)
            if (dtdetail.length % 300 == 0) {
                saveDetailExcel(dtdetail, page)
            }
            if (dtdetail.length == 15702) {
                saveDetailExcel(dtdetail, page)
            } else {
                syncdetail()
            }
        }
    }).catch(function (err) {
        console.log(err)
        setTimeout(function () {
            GetDetail(id)
        }, Math.random() * 10 * 1000)
    })
}

function iGetInnerText(testStr) {
    //console.log()
    if (testStr == null) {
        return ''
    } else {
        var resultStr = testStr.replace(/\ +/g, ""); //去掉空格
        resultStr = resultStr.replace(/[ ]/g, "");    //去掉空格
        resultStr = resultStr.replace(/[\t\r\n]/g, ""); //去掉回车换行
        resultStr = resultStr.replace(/[ ]/g, "");    //去掉空格
        return resultStr;
    }
}



function saveExcel(data, page) {
    d = (new Date()).Format('hhmmss')
    var file = xlsx.build([{ name: "mySheetName", data: data }])
    fs.writeFileSync('result_' + page + '_' + d + '.xlsx', file, 'binary')
}

function saveDetailExcel(data, page) {
    d = (new Date()).Format('hhmmss')
    var file = xlsx.build([{ name: "mySheetName", data: data }])
    fs.writeFileSync('resultDetail_' + page + '_' + d + '.xlsx', file, 'binary')
}


main()



